//
//  MyBuyViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/11.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "MyBuyViewController.h"
#import "ListTableViewCell.h"
#import "OrderFooter.h"
#import "OrderHeader.h"
#import "OrderXQViewController.h"
#import "ComplaintViewController.h"
#import "IGMyorderListApi.h"
#import "OrderDetailModel.h"
static NSString *header = @"header";
static NSString *footer = @"footer";
@interface MyBuyViewController () <UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSString *pageNumber;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@property(nonatomic,strong)IGMyorderListApi *myBuyApi;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation MyBuyViewController

#pragma mark -懒加载
-(NSMutableArray *)dataArray
{
    if (!_dataArray)
    {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark -生命周期
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.table.backgroundColor =  [UIColor c2Color];
    
    //注册监听-支付宝
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dealAlipayResult:) name:@"alipayResult" object:nil];
    //注册监听-微信
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dealWXpayResult:) name:@"WXpayresult" object:nil];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.table.width, 10)];
    view.backgroundColor = self.table.backgroundColor;
    self.table.tableHeaderView = view;
    [self.table registerClass:[ListTableViewCell class] forCellReuseIdentifier:@"oredrCell"];
    [self.table registerClass:[OrderHeader class] forHeaderFooterViewReuseIdentifier:header];
    [self.table registerClass:[OrderFooter class] forHeaderFooterViewReuseIdentifier:footer];
    [self.table ig_setHeaderWithRefreshingBlock:^{
        [self firstPage];
    }];
    self.table.emptyDataSetSource = self;
    self.table.emptyDataSetDelegate = self;
    self.myBuyApi = [IGMyorderListApi new];
    [self firstPage];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self firstPage];
}

- (void)dealAlipayResult:(NSNotification*)notification {
    [self firstPage];
}

- (void)dealWXpayResult:(NSNotification*)notification {
    [self firstPage];
}

- (void)firstPage
{
    pageNumber = @"1";
    [self requestDate];
}

-(void)requestDate
{
    _myBuyApi.type=@"2";
    _myBuyApi.pageNumber = pageNumber;
    _myBuyApi.pageSize = @"10";
    @KWeakObj(self)
    [self.myBuyApi startWithDecodeClass:[IGMyorderListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [self.table ig_endHeaderRefreshing];
        [self.table ig_endFooterRefreshing];
        if (responseObject) {
            IGMyorderListResp *resp=responseObject;
            IGMyorderListModel *model= resp.data;
            if (model.orders && model.orders.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [selfWeak.table ig_endFooterRefreshingWithNoMoreData];
            }
            if ([pageNumber isEqualToString:@"1"])
            {
                _dataArray = [NSMutableArray arrayWithArray:model.orders];
            }
            else
            {
                [_dataArray addObjectsFromArray:model.orders];
            }
            [self.table reloadData];
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.table ig_setFooterWithRefreshingBlock:^{
        pageNumber = [NSString stringWithFormat:@"%ld", [pageNumber integerValue] + 1];
        [selfWeak requestDate];
    }];
}

- (void)scrollToTOP
{
    //    [self.table setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.table ig_beginHeaderRefreshing];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count?:0;    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oredrCell"];    
    if (cell==nil)
    {
        cell = [[ListTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"oredrCell"];
    }
    cell.backColor=@"1";
    cell.data=_dataArray[indexPath.section];
    [cell setNeedsLayout];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    OrderDetailModel *info=_dataArray[indexPath.section];
        OrderXQViewController *vc = [[OrderXQViewController alloc] init];
        vc.isMyBuy = YES;
        vc.oid = info.oId;
        [UIViewController ig_pushViewController:vc animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    OrderHeader *head = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
    if (!head)
    {
        head = [[OrderHeader alloc] initWithReuseIdentifier:header];
    }
    head.isMyBuy = YES;
    head.info = _dataArray[section];
    [head setNeedsLayout];
    return head;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    OrderFooter *foot = [tableView dequeueReusableHeaderFooterViewWithIdentifier:footer];
    if (!foot)
    {
        foot = [[OrderFooter alloc] initWithReuseIdentifier:footer];
    }
    foot.isMyBuy = YES;
    foot.info = _dataArray[section];
    __weak typeof(self) weakSelf = self;
    foot.accept = ^{
        [weakSelf requestDate];
    };
    [foot setNeedsLayout];
    return foot;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
//    return [[NSAttributedString alloc] initWithString:@"您还没有购买任何物品~" attributes:nil];
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"No Purchase Yet", nil) attributes:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
